<template>
  <div class="container-xxl flex-grow-1 container-p-y">
    <div class="search-hide">
      <a v-on:click="searchShow = !searchShow">
        <i :class="searchShow?'el-icon-arrow-up':'el-icon-search'"></i>
      </a>
    </div>
    <div v-if="searchShow">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="业务员姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入业务员姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input
            v-model="queryParams.contactPhone"
            placeholder="请输入联系电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="5" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['staff:staffinfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['staff:staffinfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['staff:staffinfo:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['staff:staffinfo:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="staffinfoList" @sort-change="handleSortChange"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="业务员姓名" sortable="custom" align="center" prop="name"/>
      <el-table-column label="联系电话" align="center" prop="contactPhone"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['staff:staffinfo:edit']"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['staff:staffinfo:remove']"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务员信息对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" :width="dialogWidth" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :gutter="10" :span="12" :xs="24">
            <el-form-item label="业务员姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入业务员姓名"/>
            </el-form-item>
          </el-col>
          <el-col :gutter="10" :span="12" :xs="24">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="4" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listStaffinfo,
    getStaffinfo,
    delStaffinfo,
    addStaffinfo,
    updateStaffinfo,
    exportStaffinfo
  } from "@/api/staff/staffinfo";

  export default {
    name: "Staffinfo",
    data() {
      return {
        // 显示搜索区域
        searchShow: true,
        // 显示drawer相关设置
        showDrawer: false,
        drawerWidth: document.documentElement.clientWidth < 800 ? "72%" : "67%",
        showDrawerIndex: 0,
        drawerStyle: 'overflow-y: scroll;padding: 10px;height: ' + (document.documentElement.clientHeight - 80) + 'px;',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // dialogWidth
        dialogWidth: document.documentElement.clientWidth < 800 ? "320px" : "700px",
        // 总条数
        total: 0,
        // 业务员信息表格数据
        staffinfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          contactPhone: null,
          orderByColumn: 'createTime',
          isAsc: 'desc',
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "名字不能为空", trigger: "blur" }
          ],
          contactPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ]
        }
      };
    },
    created() {
      window.addEventListener('resize', this.handleResize)
      this.getList();
    },
    beforeDestroy: function () {
      window.removeEventListener('resize', this.handleResize)
    },
    methods: {
      /** 查询业务员信息列表 */
      getList() {
        this.loading = true;
        listStaffinfo(this.queryParams).then(response => {
          this.staffinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          deptId: null,
          name: null,
          contactPhone: null,
          contactPhone2: null,
          remark: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      handlePrevRowClick() {
        --this.showDrawerIndex
        this.form = this.staffinfoList[this.showDrawerIndex - 1];
      },
      handleNextRowClick() {
        ++this.showDrawerIndex
        if (this.staffinfoList.length >= this.showDrawerIndex) {
          this.form = this.staffinfoList[this.showDrawerIndex - 1];
        }
      },
      handleShowDetailClick(row, index) {
        this.showDrawerIndex = 1 + index
        this.form = row;
        this.showDrawer = true;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加业务员信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getStaffinfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改业务员信息";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateStaffinfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addStaffinfo(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除业务员信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delStaffinfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有业务员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportStaffinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      },
      /** 弹出对话框的屏幕适应 */
      handleResize(event) {
        if (this.dialogWidth === '700px') {
          if (document.body.clientWidth < 800) {
            this.dialogWidth = "320px"
          }
        } else if (document.body.clientWidth >= 800) {
          this.dialogWidth = '700px'
        }
      },
      handleSortChange(e) {
        if (e.order === "descending") {
          this.queryParams.orderByColumn = e.prop
          this.queryParams.isAsc = 'desc'
        } else if (e.order === "ascending") {
          this.queryParams.orderByColumn = e.prop
          this.queryParams.isAsc = 'asc'
        } else {
          this.queryParams.orderByColumn = null
          this.queryParams.isAsc = null
        }
        this.getList()
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .el-dialog__body {
    padding: 20px 20px 5px 20px;
    border-style: solid;
    border-width: 0px 0 2px 0;
    border-color: whitesmoke;
  }

  ::v-deep .el-dialog__footer {
    padding: 12px 20px;
  }

  @media (max-width: 480px) {
    ::v-deep .el-dialog__body {
      padding: 20px 20px 5px 20px;
      border-style: solid;
      border-width: 0px 0 2px 0;
      border-color: whitesmoke;
    }
    ::v-deep .el-dialog__footer {
      padding: 12px 20px;
    }
    ::v-deep .el-pagination__sizes {
      margin: 0;
    }
    ::v-deep .el-pagination .el-select .el-input {
      width: 85px;
      margin: 0;
    }
    ::v-deep .el-pagination__total {
      margin-right: 2px;
    }
    ::v-deep .el-pagination__jump {
      display: none !important;
    }
  }
</style>
